
package com.yimixian.app.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;

public class CustomViewUtils {

    /**
     * If this view does not already have a background, ensure that it sets one
     * based on the included themeAttributeResId
     * 
     * @param context The current Context
     * @param view The view to ensure a background on
     * @param themeAttributeResId The theme attribute containing reference the
     *            proper background
     * @return true if the background was set, false if the view already had a
     *         background
     */
    public static boolean ensureBackgroundOnViewFromThemeAttribute(
            Context context,
            View view,
            int themeAttributeResId) {
        if (view.getBackground() == null) {
            // Set background based on theme's definition of
            // feedStoryItemGenericBackground, but
            // only if it has not been set in xml already
            TypedValue backgroundResourceValue = new TypedValue();
            context.getTheme().resolveAttribute(
                    themeAttributeResId,
                    backgroundResourceValue, true);
            int backgroundResourceId = backgroundResourceValue.resourceId;
            view.setBackgroundResource(backgroundResourceId);
            return true;
        }
        return false;
    }

    public static void setViewPaddingFromDimens(
            View view,
            int leftPaddingDimenId,
            int topPaddingDimenId,
            int rightPaddingDimenId,
            int bottomPaddingDimenId) {

        Resources resources = view.getContext().getResources();
        int left = resources.getDimensionPixelSize(leftPaddingDimenId);
        int top = resources.getDimensionPixelSize(topPaddingDimenId);
        int right = resources.getDimensionPixelSize(rightPaddingDimenId);
        int bottom = resources.getDimensionPixelSize(bottomPaddingDimenId);
        view.setPadding(left, top, right, bottom);
    }

    public static void setViewBackground(View view, Drawable background) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(background);
        } else {
            view.setBackgroundDrawable(background);
        }
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("DeprecatedMethod")
    public static void removeOnGlobalLayoutListener(
            final View view,
            final ViewTreeObserver.OnGlobalLayoutListener listener) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
        } else {
            view.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
        }
    }

    public static Rect getViewRectRelativeToScreen(View view) {
        int[] position = new int[2];
        view.getLocationOnScreen(position);
        int left = position[0];
        int top = position[1];
        int right = left + view.getWidth();
        int bottom = top + view.getHeight();
        return new Rect(left, top, right, bottom);
    }

    public static Rect relativize(Rect rect, View reference) {
        Rect ref = getViewRectRelativeToScreen(reference);
        int left = rect.left - ref.left;
        int top = rect.top - ref.top;
        int right = left + rect.width();
        int bottom = top + rect.height();
        return new Rect(left, top, right, bottom);
    }

    public static void positionLayoutToRect(RelativeLayout.LayoutParams layout, Rect rect) {
        layout.leftMargin = rect.left;
        layout.topMargin = rect.top;
        layout.bottomMargin = 0;
        layout.rightMargin = 0;
        layout.width = rect.width();
        layout.height = rect.height();
    }

    public static boolean viewIsWiredIn(View view) {
        Object parent = view.getParent();
        while (parent instanceof View) {
            parent = ((View) parent).getParent();
        }
        return parent != null;
    }
}
